<?php
// <editor-fold defaultstate="collapsed" desc="includes">
include_once ('../includes/db.php');
include_once ('../includes/class.krumo.php');
include_once ('../includes/constants.php');
include_once ('../includes/usuario.php');
include_once '../includes/publicidad.php';

$db = new db();
$ad = new publicidad();
$usuario = new usuario();
$usuario->confirmar_miembro();
//// </editor-fold>
// <editor-fold defaultstate="collapsed" desc="Select">
$tipospublicacion = $db->dame_query("select * from tipopublicidad");
//// </editor-fold>
// <editor-fold defaultstate="collapsed" desc="Operaciones">
if (isset($_POST['enviar'])) {
    $data = $_POST;
    $data['imagen'] = $_FILES;
    unset($data['enviar']);
    $result = $ad->registrar($data);
    if (!$result) {
        trigger_error("No se pudo registrar la publicidad: " . var_export($result, 1), E_USER_ERROR);
    }
}
//// </editor-fold>
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
    <head>
        <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
        <meta http-equiv="Content-language" content="en" />
        <title>Registrar Anuncio :: CCIVenezuela.com </title>
        <link href="../css/style.css" media="all" rel="stylesheet" type="text/css"/>
        <script type="text/javascript" language="javascript"  src="../js/jquery-1.4.2.min.js"></script>
        <script type="text/javascript" src="../js/index.js"></script>
        <script type="text/javascript" language="javascript"  src="../js/jquery-validate/jquery.validate.min.js"></script>
        <script type="text/javascript" language="javascript" src="../js/jquery-validate/localization/messages_es.js"></script>
        <script type="text/javascript">
            $(document).ready(function(){
                $.validator.addMethod("alphanumeric", function(value, element) {
                    return this.optional(element) || /^[a-z0-9\-]+$/i.test(value);
                }, "Este campo debe tener solo letras numeros y guiones.");
                //                            $("#registrar").validate();
                $("#registrar_ad").validate({
                    rules: {
                        placa:{
                            alphanumeric:true
                        },
                        foto1: {
                            required: true,
                            accept: "jpg|png"
                        },
                        foto2: {
                            accept: "jpg|png"
                        },
                        foto3: {
                            accept: "jpg|png"
                        },
                        foto4: {
                            accept: "jpg|png"
                        },
                        foto5: {
                            accept: "jpg|png"},
                        caracteristicas:{
                            maxlength: 500
                        }
                    }
                });
            });
        </script>
        <!--[if lte IE 7]>
        <link href="../css/patches/patch_my_layout.css" rel="stylesheet" type="text/css" />
        <![endif]-->
        <style media="all" type="text/css">
            #col2{width: 100%; padding: 0;}
            form.columnar div.type-text input[type="text"]{
                width: 66.8%;
            }
        </style>
    </head>
    <body>
        <div id="header">
            <?php include_once SERVER_ROOT . '/template/header.php'; ?>
        </div>
        <div id="content">
            <div>
                <?php if (!isset($_POST['enviar']) | (isset($exito) && !$exito)): ?>
                    <form id="registrar_ad" action="<? echo $_SERVER['PHP_SELF']; ?>" class="yform columnar" method="post" enctype="multipart/form-data">
                        <fieldset>
                            <input type="hidden" name="usuario_id" value="<?php echo $_SESSION['usuario']['id']; ?>"/>
                            <legend>Datos de la publicación</legend>
                            <div class="subcolumns">
                                <div class="c50l">
                                    <div class="type-text">
                                        <label for="nombre">nombre<sup>*</sup> </label>
                                        <input type="text" name="nombre" class="required"/>
                                    </div>
                                    <div class="type-text">
                                        <label for="url">Url<sup>*</sup> </label>
                                        <input type="text" name="url" class="required"/>
                                    </div>
                                    <div class="type-text">
                                        <label for="foto">Imagen <sup>*</sup> </label>
                                        <input type="file" name="foto" class="required"/>
                                    </div>
                                </div>
                                <div class="c50r">
                                    <div class="type-select">
                                        <label for="tipo">Tipo de anuncio <sup>*</sup> </label>
                                        <select name="tipo" class="required">
                                            <?php foreach ($tipospublicacion['data'] as $tipo): ?>
                                                <option value="<?php echo $tipo['id'] ?>"><?php echo $tipo['nombre'] ?></option>
                                            <?php endforeach; ?>
                                        </select>
                                    </div>
                                    <div class="type-select">
                                        <label for="status">Status</label>
                                        <select name="status">
                                            <option value="1" selected="selected">Activo</option>
                                            <option value="0">Inactivo</option>
                                        </select>
                                    </div>
                                </div>
                            </div>
                        </fieldset>
                        <fieldset>
                            <legend>Acción</legend>
                            <div class="type-button">
                                <p align="right">
                                    <input type="reset" name="borrar" value="Resetear"/>
                                    <input type="submit" name="enviar" value="Enviar"/>
                                </p>
                            </div>
                        </fieldset>
                    </form>
                <?php else: ?>
                    <div class="floatbox">
                        <div class="note">
                            <p class="info">
                                <span>Registro agregado con éxito.</span>
                            </p>
                        </div>
                    </div>
                <?php endif; ?>
                <div class="floatbox">
                    <center>
                        <a href="../usuarios">Volver al menú</a>
                    </center>
                </div>
            </div>
        </div>
        <div id="footer">
            <?php include SERVER_ROOT . '/template/footer.php'; ?>
        </div>
    </body>
</html>